Welcome Guest | Sign in | Register

Home > Java Programming > Static Concept > Questions and Answers

Exercise:

Section 1

01. class A{

Static int i=10;
Static int j=I;
Public static void main(String args[]){
System.out.println(i);
System.out.println(j);
}
}
A. compile time error B. run time error

C. 10 10 D. no output

Answer and Explanation

Answer: 10 10

Explanation:
Here I and j are static global variable ,these static variables are accessed inside a main() method, as these are static variable no need to create an object we can access directly because for static variables memory will be allocated automatically. So the value of I and j will displayed inside main() method.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. class J
{
Static
{
System.out.println(“first”);
}
Public static void main(String args[])
{
System.out.println(“main”);
}
}
A. first
main
B. main
first
C. compile time error D. runtime error

Answer and Explanation

Answer: first

main

Explanation:
here we have declared SIB(static initialization block), JVM always executes SIB first , then main() method , then it executes main() method, so ans a) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. class J{
Static int I;
Static
{
System.out.println(i);
I=100;
}
Public static void main(String args[])
{
System.out.println(i);
}
Static
{
System.out.println(i);
I=200;
}

}
A. 0 200 100 B. 0 100 200
C. compile time error D. 200 100 0

Answer and Explanation

Answer: 0 100 200

Explanation:
here we have declared two SIB(static initialization block), JVM always executes SIB first before the main() method , then it executes main() method,if you have more than one SIB then JVM will execute in the order in which they appear the program , and if you declare a global variable and don’t initialize it then it will take default value depending on the datatype. So in first SIB default value of I will be displayed , and I will be initialized to 100, and in second SIB 100 will displayed and initialized to 200 then in main() 200 will be displayed. So ans b) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. class A{

Static{
Main(null);}
Public static void main(String args[]){
System.out.println(“main”);}
}
A. compile time error B. runtime error
C. main main  D. exception

Answer and Explanation

Answer: main main 

Explanation:
Explanation: here we have declared SIB(static initialization block), JVM always executes SIB first before the main() method , then it executes main() method, here from SIB they are calling main() method by passing null as parameter so because of calling main() in SIB it displays “main” , then JVM executes main() method so again “main” will be displayed . so ans c) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. class A{

Static{
String all[]=new String[0];
Main(null);
}
Public static void main(String args[]){
System.out.println(“main”);}
}
A. compile time error B. runtime error
C. main main  D. exception

Answer and Explanation

Answer: main main 

Explanation:
here we have declared SIB(static initialization block), JVM always executes SIB first before the main() method , then it executes main() method,they have declared string array with size 0, in 0th position null will be stored. here from SIB they are calling main() method by passing null as parameter so because of calling main() in SIB it displays “main” , then JVM executes main() method so again “main” will be displayed . so ans c) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. class c{
Int I;
Static void test1()
{
System.out.println(“test1”);
}
Static void tes2t()
{
System.out.println(“test1”);
}

}
A. compile time error B. runtime exception
C. test1 test2  D. no output

Answer and Explanation

Answer: runtime exception

Explanation:
here we declared static method not SIB, JVM try to execute but there is no main() method so it will throw runtime exception

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. class A{
Int I;
Static{
A a=new A();
System.out.println(a.i);
}
}
A. compile time error B. compile time error
C. runtime error D. no output

Answer and Explanation

Answer: compile time error

Explanation:
JVM will execute SIB first , it will display default value of i. then JVM will come to know that there is no main method so it will throw an exception.so ans b) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. class G{
Int I;
}

Class Manager{
Public static void main(String args[]){
System.out.println(G.i);
}
}
A. compile time error B. 0 and runtime error
C. runtime error D. no output

Answer and Explanation

Answer: compile time error

Explanation:
here variable I is non-static ,this variable can not be accessed from static function main(), if you try it will throw an compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.